package com.yuyou.fn.tradingservice.service.impl;

import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.tradingservice.entity.ServiceCart;
import com.yuyou.fn.tradingservice.entity.ServiceImage;
import com.yuyou.fn.tradingservice.entity.ServiceProduct;
import com.yuyou.fn.tradingservice.service.IServiceCartService;
import com.yuyou.fn.tradingservice.service.IServiceImageService;
import org.springframework.stereotype.Service;

import com.yuyou.fn.tradingservice.mapper.ServiceCartItemMapper;
import com.yuyou.fn.tradingservice.entity.ServiceCartItem;
import com.yuyou.fn.tradingservice.service.IServiceCartItemService;
import com.github.wens.framework.service.impl.SuperServiceImpl;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 *
 * ServiceCartItem 表数据服务层接口实现类
 *
 */
@Service
public class ServiceCartItemServiceImpl extends SuperServiceImpl<ServiceCartItemMapper, ServiceCartItem,Long> implements IServiceCartItemService {

    @Resource
    private IServiceImageService imageService;

    @Resource
    private IServiceCartService cartService;

    @Override
    public void addToCartItem(ServiceProduct product, Long cartId, int quantity) {
        ServiceCartItem cartItem = new ServiceCartItem();
        BeanUtils.copyProperties(cartItem, product);
        cartItem.setQuantity(quantity);
        cartItem.setCartId(cartId);
        BigDecimal quantityBig = new BigDecimal(quantity);
        cartItem.setPrice(product.getPrice().multiply(quantityBig));
        //添加第一张图片
        ServiceImage image = imageService.findFirstImage(product.getProductId());
        if(image != null) {
            String imageUrl = image.getImageUrl();
            if(!"".equals(imageUrl) && imageUrl != null) {
                cartItem.setImageUrl(imageUrl);
            }
        }
        insertSelective(cartItem);
    }

    @Override
    public void addRentToCartItem(ServiceProduct product, Long cartId, String startDate, String endDate, String startTime, String endTime) {
        ServiceCartItem cartItem = new ServiceCartItem();
        BeanUtils.copyProperties(cartItem, product);
        cartItem.setCartId(cartId);
        cartItem.setPrice(product.getPrice());
        //计算时间数
        cartItem.setRentSpan(TimeUtils.hoursCounting(startDate, endDate, startTime, endTime, "yyyy-MM-dd","HH:mm"));
        cartItem.setStartDate(startDate);
        cartItem.setEndDate(endDate);
        cartItem.setStartTime(startTime);
        cartItem.setEndTime(endTime);
        //添加第一张图片
        ServiceImage image = imageService.findFirstImage(product.getProductId());
        if(image != null) {
            String imageUrl = image.getImageUrl();
            if(!"".equals(imageUrl) && imageUrl != null) {
                cartItem.setImageUrl(imageUrl);
            }
        }
        insertSelective(cartItem);
    }

    @Override
    public void updateCartItem(Long cartItemId, int quantity) {
        ServiceCartItem cartItem = this.findById(cartItemId);
        cartItem.setPrice(cartItem.getPrice().divide(new BigDecimal(cartItem.getQuantity())).multiply(new BigDecimal(quantity)));
        cartItem.setQuantity(quantity);
        this.updateSelectiveById(cartItem);
    }

    @Override
    public void delServiceCartOrNot(Integer type) {
        Example<ServiceCartItem> example = ServiceCartItem.createExample();
        example.createCriteria().andEqualTo("type", type);
        List<ServiceCartItem> cartItemList = this.findListByExample(example);
        if(cartItemList.size() == 0) {
            Example<ServiceCart> exampleCart = ServiceCart.createExample();
            exampleCart.createCriteria().andEqualTo("type", type);
            cartService.deleteByExample(exampleCart);
        }
    }

    @Override
    public List<ServiceCartItem> getAllCartItems(Integer type) {
        Example<ServiceCartItem> example = ServiceCartItem.createExample();
        example.createCriteria().andEqualTo("type", type);
        return this.findListByExample(example);
    }
}